<template>
<el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" top="80px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="170px">
        <el-form-item prop="productName" label="商品名称">
            <el-input v-model="dataForm.productName" placeholder=""></el-input>
        </el-form-item>
        <!-- <el-form-item label="商户" prop="businessInd">
            <el-select v-model="dataForm.businessInd" placeholder="请选择商户" clearable style="width:100%">
                <el-option :label="item.name" :value="index" v-for="(item,index) in business" :key="index">
                </el-option>
            </el-select>
        </el-form-item> -->
        <!-- <el-form-item prop="type" label="类型">
            <el-radio-group v-model="dataForm.type">
                <el-radio :label="0">菜价 </el-radio>
                <el-radio :label="1">农副产品</el-radio>
            </el-radio-group>
        </el-form-item> -->
        <el-form-item prop="clzMoney" label="菜篮子工程服务部价格">
            <el-input v-model="dataForm.clzMoney" placeholder="" type="number" min="0"></el-input>
        </el-form-item>
        <el-form-item prop="individualityMoney" label="集贸市场个体摊位价格">
            <el-input v-model="dataForm.individualityMoney" placeholder="" type="number" min="0"></el-input>
        </el-form-item>
        <template v-if="dataForm.type==0">
            <el-form-item prop="ptdhMoney" label="普陀东河菜场价格">
                <el-input v-model="dataForm.ptdhMoney" placeholder="" type="number" min="0"></el-input>
            </el-form-item>
            <el-form-item prop="dhnzMoney" label="定海南珍菜场价格">
                <el-input v-model="dataForm.dhnzMoney" placeholder="" type="number" min="0"></el-input>
            </el-form-item>
            <el-form-item prop="dsgtMoney" label="岱山高亭菜场价格">
                <el-input v-model="dataForm.dsgtMoney" placeholder="" type="number" min="0"></el-input>
            </el-form-item>
        </template>
        <el-form-item prop="productInformation" label="商品信息">
            <el-input type="textarea" rows="4" v-model="dataForm.productInformation" placeholder=""></el-input>
        </el-form-item>
    </el-form>
    <template slot="footer">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
</el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: "",
                businessAddress: "",
                businessId: '',
                businessName: "",
                productName: "",
                clzMoney: "",
                individualityMoney: "",
                productInformation: "",
                ptdhMoney: '',
                dhnzMoney: '',
                dsgtMoney: '',
                type: '',
                businessInd: ''
            },
            business: [],
        };
    },
    props: ['type', 'id'],
    computed: {
        dataRule() {
            return {
                businessInd: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }],
                productName: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }],
                clzMoney: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }],
                ptdhMoney: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }],
                dhnzMoney: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }],
                dsgtMoney: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }],
                individualityMoney: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
                productInformation: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
            };
        },
    },
    methods: {
        getBusiness() {
            this.$http.get('/scenic/fisherman/page', {
                params: {
                    page: 1,
                    limit: 200,
                    status: 1
                }
            }).then(res => {
                if (res.data.code == 0) {
                    this.business = res.data.data.list;
                    if (this.dataForm.id) {
                        this.getInfo();
                    }
                }

            })
        },
        init() {
            this.dataForm.type = this.type;
            this.visible = true;
            this.$nextTick(() => {
                this.dataForm.businessAddress = "";
                this.dataForm.businessId = '';
                this.dataForm.businessName = "";
                this.$refs["dataForm"].resetFields();
                this.getBusiness();

            });
        },
        // 获取信息
        getInfo() {
            this.$http
                .get(`/scenic/newsProduct/${this.dataForm.id}`)
                .then(({
                    data: res
                }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    this.dataForm = {
                        ...this.dataForm,
                        ...res.data,
                    };
                    let ind = this.business.findIndex((item) => item.id == this.dataForm.businessId);
                    this.dataForm.businessInd = ind < 0 ? '' : ind;
                })
                .catch(() => {});
        },
        // 表单提交
        dataFormSubmitHandle: debounce(
            function () {
                this.$refs["dataForm"].validate((valid) => {
                    if (!valid) {
                        return false;
                    }
                    this.dataForm.businessAddress = this.business[this.dataForm.businessInd].address;
                    this.dataForm.businessId = this.business[this.dataForm.businessInd].id;
                    this.dataForm.businessName = this.business[this.dataForm.businessInd].name;
                    this.$http[!this.dataForm.id ? "post" : "put"](
                            "/scenic/newsProduct",
                            this.dataForm
                        )
                        .then(({
                            data: res
                        }) => {
                            if (res.code != 0) {
                                return this.$message.error(res.msg);
                            }
                            this.$message({
                                message: this.$t("prompt.success"),
                                type: "success",
                                duration: 500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit("refreshDataList");
                                },
                            });
                        })
                        .catch(() => {});
                });
            },
            1000, {
                leading: true,
                trailing: false
            }
        ),
    },
};
</script>
